package pos.gengshen.com.gspos.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import pos.gengshen.com.gspos.R;
import pos.gengshen.com.gspos.common.Constant;
import pos.gengshen.com.gspos.utils.HttpUtils;

/**
 * Created by Administrator on 2018/7/11/011.
 * 登录
 *
 */



public class LoginActivity extends Activity{

    private EditText nameet,passwordet;
    private Button login;
    private String url = Constant.SERVER_URL+"/admin/staff/login";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_login);
        nameet = (EditText)findViewById(R.id.name);
        passwordet = (EditText)findViewById(R.id.password);
        login = (Button)findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = nameet.getText().toString();
                String pwd = passwordet.getText().toString();
                if(name.equals("")){
                    System.out.println("请输入用户名");
                    Toast.makeText(getApplicationContext(),"请输入用户名",Toast.LENGTH_SHORT).show();
                }else if(pwd.equals("")){
                    System.out.println("请输入密码");
                    Toast.makeText(getApplicationContext(),"请输入密码",Toast.LENGTH_SHORT).show();
                }else{

                    Map<String,String> map = new HashMap<>();
                    map.put("name",name);
                    map.put("pwd",pwd);
                    System.out.println("========"+url);
                    HttpUtils.doPost(url, null, map, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                            System.out.println("请求失败");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(),"网络出错",Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            final String result = response.body().string();
                            System.out.println("登录请求成功");
                            if(response != null){
                                JSONObject jsonObject = JSON.parseObject(result);
                                String code = jsonObject.getString("code");
                                if(code.equals("200")){
                                    Boolean obj = jsonObject.getBoolean("obj");
                                    if(obj){
                                        System.out.println("登录成功");
                                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                        startActivity(intent);
                                    }else {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(getApplicationContext(),"账号或密码错误",Toast.LENGTH_SHORT).show();
                                            }
                                        });

                                    }
                                }else{
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getApplicationContext(),"账号或密码错误",Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            }

                        }
                    });


                }




            }
        });






    }
}
